/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * timer1.java
 *
 * Created on 13-gen-2010, 13.35.38
 */

/**
 *
 * @author Paolo
 */
public class timer1 extends javax.swing.JFrame {
    private int valore;
    private int scelta;

    /** Creates new form timer1 */
    public timer1() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup2 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jProgressBar1 = new javax.swing.JProgressBar();
        jComboBox1 = new javax.swing.JComboBox();
        jSpinner1 = new javax.swing.JSpinner();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        jLabel1.setText("In cosa vuoi impostare il tempo?");

        jProgressBar1.setAutoscrolls(true);
        jProgressBar1.setDoubleBuffered(true);
        jProgressBar1.setOpaque(true);

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Scegli", "Secondi", "Minuti", "Ore" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jSpinner1.setEnabled(false);
        jSpinner1.setOpaque(false);

        jLabel2.setOpaque(true);

        jButton1.setText("Start!");
        jButton1.setEnabled(false);
        jButton1.setOpaque(false);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jProgressBar1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 176, Short.MAX_VALUE)
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel1)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 55, Short.MAX_VALUE)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jComboBox1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        int scegli=this.jComboBox1.getSelectedIndex();
        this.scelta=scegli;
        if(scegli==0){
            this.jLabel2.setText("");
            this.jSpinner1.setOpaque(false);
            this.jSpinner1.setEnabled(false);
            this.jButton1.setOpaque(false);
            this.jButton1.setEnabled(false);
        }
        if(scegli==1){
            this.jLabel2.setText("Quanti secondi deve durare?");
            this.jSpinner1.setOpaque(true);
            this.jSpinner1.setEnabled(true);
            this.jButton1.setOpaque(true);
            this.jButton1.setEnabled(true);
        }else if(scegli==2){
            this.jLabel2.setText("Quanti minuti deve durare?");
            this.jSpinner1.setOpaque(true);
            this.jSpinner1.setEnabled(true);
            this.jButton1.setOpaque(true);
            this.jButton1.setEnabled(true);

        }else if(scegli==3){
            this.jLabel2.setText("Quante ore deve durare?");
            this.jSpinner1.setOpaque(true);
            this.jSpinner1.setEnabled(true);
            this.jButton1.setOpaque(true);
            this.jButton1.setEnabled(true);
        }
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        int valor=0;
        if(this.scelta==1){
            Object value=this.jSpinner1.getValue();
            String val=value.toString();
            valor= Integer.parseInt(val);
            this.valore=valor*1000;
        }else if(this.scelta==2){
            Object value=this.jSpinner1.getValue();
            String val=value.toString();
            valor= Integer.parseInt(val);
            this.valore=valor*1000*60;
        }else if(this.scelta==3){
            Object value=this.jSpinner1.getValue();
            String val=value.toString();
            valor= Integer.parseInt(val);
            this.valore=valor*1000*60*60;
        }
        this.jProgressBar1.setMaximum(this.valore);
        this.jProgressBar1.setValue(0);
        this.jLabel3.setText("");
        long inizio = System.currentTimeMillis();
        long fine = System.currentTimeMillis();
        long differenza = fine - inizio;
        while (differenza < this.valore) {
            fine = System.currentTimeMillis();
            differenza = fine - inizio;
            int diff=(int) differenza;
            this.jProgressBar1.setValue(diff);
        }
        this.jLabel3.setText("Tempo terminato!");
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new timer1().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JSpinner jSpinner1;
    // End of variables declaration//GEN-END:variables

}
